package com.open.capacity.finance.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.open.capacity.finance.entity.SourceBaseBudgetExcute;
import com.open.capacity.finance.util.FileUtil;
import com.open.capacity.user.model.SysCompany;
import com.open.capacity.user.service.SysCompanyService;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.SourceBaseHotel;
import com.open.capacity.finance.dao.SourceBaseHotelDao;
import com.open.capacity.finance.service.SourceBaseHotelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.swing.StringUIClientPropertyKey;

@Slf4j
@Service
public class SourceBaseHotelServiceImpl implements SourceBaseHotelService {

    @Autowired
    private SourceBaseHotelDao sourceBaseHotelDao;

    @Autowired
    private SysCompanyService companyService;

    /**
     * 保存到数据库
     *
     * @param url
     */
    @Transactional
    public int save2db(String companyId, Integer kjnd, Integer importway, String url) {
        MultipartFile multipartFile = FileUtil.toMultipartFile("hoteldataXls", url);
        ImportParams params = new ImportParams();
        params.setTitleRows(0); //前面header空几行
        params.setHeadRows(1);
        //params.setStartRows(2);
        // 开启excel校验
        params.setNeedVerify(false);

        SysCompany company = companyService.findById(companyId);

        List<SourceBaseHotel> list = new ArrayList<>();
        try {
            list = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                    SourceBaseHotel.class, params);
        } catch (Exception e) {
            log.error("从文件读取数据错误:{}", e.getMessage());
        }

        //删除旧数据
        if (importway == 1) {
            delete(companyId, kjnd);
        }

        for (SourceBaseHotel item : list) {
            item.setCompanyId(companyId);
            item.setCompanyName(company.getFullName());
            item.setKjnd(kjnd);
            if(StringUtils.isNotEmpty(item.getSupplier())) {
                sourceBaseHotelDao.save(item);
            }
        }

        return list.size();
    }


    /**
     * 删除
     *
     * @param companyId
     */
    public int delete(String companyId, Integer kjnd) {
        return sourceBaseHotelDao.deleteByCompanyIdAndKjnd(companyId, kjnd);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SourceBaseHotel findById(String id) {
        return sourceBaseHotelDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SourceBaseHotel> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SourceBaseHotel> list = sourceBaseHotelDao.findAll(params);
        PageInfo<SourceBaseHotel> pageInfo = new PageInfo(list);

        return PageResult.<SourceBaseHotel>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SourceBaseHotel> findAll(Map<String, Object> params) {
        return sourceBaseHotelDao.findAll(params);
    }

}
